package com.doubao.chat.config;

import com.doubao.chat.service.AIProviderService;
import com.doubao.chat.service.impl.AIProviderServiceImpl;
import com.doubao.chat.service.impl.CozeSDKProviderServiceImpl;
import com.doubao.device.service.SmartDeviceService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * 聊天服务配置类
 */
@Configuration
public class ChatServiceConfig {

    /**
     * 配置自定义RestTemplate，增加超时设置
     */
    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(30000); // 30秒连接超时
        factory.setReadTimeout(30000);    // 30秒读取超时
        return new RestTemplate(factory);
    }

    /**
     * 配置默认的AI提供商服务实现
     * 使用@Primary注解标记为首选的实现
     */
    @Bean
    @Primary
    @Qualifier("AIProviderServiceImpl")
    public AIProviderService aiProviderService(SmartDeviceService deviceService, RestTemplate restTemplate) {
        return new AIProviderServiceImpl(deviceService, restTemplate);
    }

    /**
     * 配置Coze SDK的AI提供商服务实现
     */
    @Bean
    @Qualifier("CozeSDKProviderServiceImpl")
    public AIProviderService cozeSDKProviderService(SmartDeviceService deviceService) {
        return new CozeSDKProviderServiceImpl(deviceService);
    }
}